#ifndef COMMANDREPOSITORY_H
#define COMMANDREPOSITORY_H

#include <QObject>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QString>
#include "database/database_manager.h"

// 前向声明
struct KmsCommand;

class CommandRepository
{
public:
    explicit CommandRepository(QSqlDatabase &database);
    
    // 基本CRUD操作
    bool insert(const KmsCommand &command);
    bool updateLocalFilePath(const QString &downloadUrl, const QString &localFilePath);
    KmsCommand findByLocalFilePath(const QString &localFilePath);
    KmsCommand findByLocalFilePathAndCommand(const QString &localFilePath, const QString &command);
    KmsCommand findByDownloadUrl(const QString &downloadUrl);
    bool removeByCommitUrl(const QString &commitUrl);
    bool updateStatusByCommitUrl(const QString &commitUrl, const QString &command, const QString &status);
    bool updateStatusByDownloadUrl(const QString &downloadUrl, const QString &command, const QString &status);
    
private:
    QSqlDatabase &m_database;
    
    bool isDatabaseConnected() const;
    KmsCommand mapFromQuery(const QSqlQuery &query) const;
    
    // JSON 处理辅助方法
    QString getParamFromJson(const QString &paramsJson, const QString &key) const;
    QString buildParamsJson(const QString &localFilePath, const QString &downloadUrl, 
                           const QString &commitUrl, const QString &accessToken) const;
    QString updateParamInJson(const QString &paramsJson, const QString &key, const QString &value) const;
};

#endif // COMMANDREPOSITORY_H

